/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FlooditMainGui.java
 *
 * Created on 08-nov-2011, 18:20:51
 */
package example_floodit;

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 *
 * @author BigEma
 */
public class FlooditMainGui extends javax.swing.JFrame {

    /** Creates new form FlooditMainGui */
    public FlooditMainGui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        stepsMadeLabel = new javax.swing.JLabel();
        numberStepsMadeLabel = new javax.swing.JLabel();
        botonAzul = new javax.swing.JButton();
        botonVerde = new javax.swing.JButton();
        botonAmarillo = new javax.swing.JButton();
        botonRojo = new javax.swing.JButton();
        botonCeleste = new javax.swing.JButton();
        botonRosa = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        board = new javax.swing.JTable();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newGameMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();
        iaMenu = new javax.swing.JMenu();
        pistaMenuItem = new javax.swing.JMenuItem();
        jugarMenuItem = new javax.swing.JMenuItem();
        difficultMenu = new javax.swing.JMenu();
        easyDifficulty = new javax.swing.JMenuItem();
        mediumDifficulty = new javax.swing.JMenuItem();
        hardDifficulty = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);

        stepsMadeLabel.setText("Steps made: ");

        numberStepsMadeLabel.setText("XXX");

        botonAzul.setText("azul");

        botonVerde.setText("verde");

        botonAmarillo.setText("amarillo");

        botonRojo.setText("rojo");

        botonCeleste.setText("celeste");

        botonRosa.setText("rosa");

        board.setModel(new javax.swing.table.DefaultTableModel(
            new String[14][14],
            new String[14]
        ));
        board.setDefaultRenderer(Object.class, new BoardRender());
        board.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        board.setAutoscrolls(false);
        board.setEditingColumn(14);
        board.setEditingRow(14);
        board.setEnabled(false);
        board.setRowSelectionAllowed(false);
        board.setShowHorizontalLines(false);
        board.setShowVerticalLines(false);
        board.setTableHeader(null);
        jScrollPane1.setViewportView(board);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonAzul, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(botonVerde, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(botonAmarillo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(botonRojo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(botonCeleste, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(botonRosa, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(stepsMadeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(numberStepsMadeLabel)
                        .addGap(202, 202, 202))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stepsMadeLabel)
                    .addComponent(numberStepsMadeLabel))
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(botonAzul)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonVerde)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonAmarillo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonRojo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonCeleste)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonRosa))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        fileMenu.setMnemonic('f');
        fileMenu.setText("Floodit");

        newGameMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newGameMenuItem.setMnemonic('N');
        newGameMenuItem.setText("Nuevo");
        fileMenu.add(newGameMenuItem);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setMnemonic('x');
        exitMenuItem.setText("Salir");
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setMnemonic('h');
        helpMenu.setText("Ayuda");

        aboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        aboutMenuItem.setMnemonic('a');
        aboutMenuItem.setText("Acerca de");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        iaMenu.setMnemonic('I');
        iaMenu.setText("IA");

        pistaMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        pistaMenuItem.setMnemonic('P');
        pistaMenuItem.setText("Pista");
        iaMenu.add(pistaMenuItem);

        jugarMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, java.awt.event.InputEvent.CTRL_MASK));
        jugarMenuItem.setMnemonic('J');
        jugarMenuItem.setText("Jugar");
        iaMenu.add(jugarMenuItem);

        menuBar.add(iaMenu);

        difficultMenu.setText("Dificultad");

        easyDifficulty.setMnemonic('f');
        easyDifficulty.setText("Fácil");
        difficultMenu.add(easyDifficulty);

        mediumDifficulty.setMnemonic('i');
        mediumDifficulty.setText("Intermedio");
        difficultMenu.add(mediumDifficulty);

        hardDifficulty.setMnemonic('d');
        hardDifficulty.setText("Difícil");
        difficultMenu.add(hardDifficulty);

        menuBar.add(difficultMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FlooditMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FlooditMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FlooditMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FlooditMainGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FlooditMainGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JTable board;
    private javax.swing.JButton botonAmarillo;
    private javax.swing.JButton botonAzul;
    private javax.swing.JButton botonCeleste;
    private javax.swing.JButton botonRojo;
    private javax.swing.JButton botonRosa;
    private javax.swing.JButton botonVerde;
    private javax.swing.JMenu difficultMenu;
    private javax.swing.JMenuItem easyDifficulty;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuItem hardDifficulty;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenu iaMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem jugarMenuItem;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem mediumDifficulty;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem newGameMenuItem;
    private javax.swing.JLabel numberStepsMadeLabel;
    private javax.swing.JMenuItem pistaMenuItem;
    private javax.swing.JLabel stepsMadeLabel;
    // End of variables declaration//GEN-END:variables


//CODIGO PROPIO

//GETTERS Y SETTERS

    public JMenuItem getAboutMenuItem() {
        return aboutMenuItem;
    }

    public JButton getBotonAmarillo() {
        return botonAmarillo;
    }

    public JButton getBotonAzul() {
        return botonAzul;
    }

    public JButton getBotonCeleste() {
        return botonCeleste;
    }

    public JButton getBotonRojo() {
        return botonRojo;
    }

    public JButton getBotonRosa() {
        return botonRosa;
    }

    public JButton getBotonVerde() {
        return botonVerde;
    }

    public JMenuItem getExitMenuItem() {
        return exitMenuItem;
    }

    public JMenu getIaMenu() {
        return iaMenu;
    }

    public JMenuItem getJugarMenuItem() {
        return jugarMenuItem;
    }

    public JMenuItem getNewGameMenuItem() {
        return newGameMenuItem;
    }

    public JLabel getNumberStepsMadeLabel() {
        return numberStepsMadeLabel;
    }

    public JMenuItem getPistaMenuItem() {
        return pistaMenuItem;
    }

    public JMenuItem getEasyDifficulty() {
        return easyDifficulty;
    }

    public JMenuItem getHardDifficulty() {
        return hardDifficulty;
    }

    public JMenuItem getMediumDifficulty() {
        return mediumDifficulty;
    }
    
    
    
    //ADD LISTENER BUTTONS
    
    public void setListenerButtons(ActionListener lis) {
        this.botonAmarillo.addActionListener(lis);
        this.botonAzul.addActionListener(lis);
        this.botonCeleste.addActionListener(lis);
        this.botonRojo.addActionListener(lis);
        this.botonRosa.addActionListener(lis);
        this.botonVerde.addActionListener(lis);
        this.newGameMenuItem.addActionListener(lis);
        this.exitMenuItem.addActionListener(lis);
        this.pistaMenuItem.addActionListener(lis);
        this.jugarMenuItem.addActionListener(lis);
        this.aboutMenuItem.addActionListener(lis);
        this.easyDifficulty.addActionListener(lis);
        this.mediumDifficulty.addActionListener(lis);
        this.hardDifficulty.addActionListener(lis);
    }
    
    //UPDATE Y AUXILIARES
    
    public void update(FlooditState model) {
        //System.out.println("ENTRE A UPDATE");
		for (int row = 0; row<model.getSize();row++) {
			for (int col = 0; col<model.getSize();col++) {
				if (model.getColorOfToken(row, col) == FlooditState.verde) {
					this.board.setValueAt("verde", row, col);
				} else if (model.getColorOfToken(row, col) == FlooditState.amarillo) {
					this.board.setValueAt("amarillo", row, col);
				} else if (model.getColorOfToken(row, col) == FlooditState.rojo) {
					this.board.setValueAt("rojo", row, col);
				} else if (model.getColorOfToken(row, col) == FlooditState.azul) {
					this.board.setValueAt("azul", row, col);
				} else if (model.getColorOfToken(row, col) == FlooditState.celeste) {
					this.board.setValueAt("celeste", row, col);
				} else if (model.getColorOfToken(row, col) == FlooditState.rosa) {
					this.board.setValueAt("rosa", row, col);
				}
			}
		}
		//System.out.println("PASO HECHOS : " + model.getSteps());
		this.numberStepsMadeLabel.setText(Integer.toString(model.getSteps()));
		if (model.success()) {
            WinLoseWindow winWindow = new WinLoseWindow(true,model.getSteps());
			winWindow.setFather(this);
            winWindow.showGui();
			enableButtons(false);
		} else if (model.isFinal()) {
            WinLoseWindow loseWindow = new WinLoseWindow(false,model.getSteps());
            loseWindow.setFather(this);
            loseWindow.showGui();
			enableButtons(false);
		}
    }
    
    public void enableButtons(boolean enable) {
		this.botonAzul.setEnabled(enable);
		this.botonCeleste.setEnabled(enable);
		this.botonVerde.setEnabled(enable);
		this.botonAmarillo.setEnabled(enable);
		this.botonRojo.setEnabled(enable);
		this.botonRosa.setEnabled(enable);
	}

}
